layui.use(['tableEdit', 'table', 'form', 'laydate',"selectInput","excel"], function () {
    let layer = layui.layer //弹层
        , table = layui.table //表格
        , form = layui.form,
        $ = layui.jquery,
        laydate = layui.laydate,
        excel = layui.excel,
        selectInput = layui.selectInput;
    lay('.starttime').each(function () {
        laydate.render({
            elem: this
        });
    });
    function getAllJson(jsons) {
        let tmpitem=[]
        for (let item of jsons) {
            let tmparr={}
            tmparr["value"]=item["code"];
            tmparr["name"]=item["name"];
            if(item["users"]){
                for(let it of item["users"]){
                    item["list"].push(it)
                }
            }
            if(item["list"] && item["list"].length){
                tmparr["children"]=   getAllJson(item["list"]);
                tmpitem.push(tmparr)
            }else{
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }

    function getxmselect(el,name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon:true,
                indent: 20,
                expandedKeys:false
            },
            height: 'auto',
            data: data,
            on: function ({ arr, change, isAdd }) {
                return this
            }
        })
    }



    let operationList=[];
    let deptOperationList=[];
    layer.ready(function () {
        //供应商
        $.ajax({
            url: "customer2/getAllInspectUnit",
            success: function (res) {
                if (res.status == 200) {
                    $.each(res.data, function (index, item) {
                        $("#supplierCode").append(new Option(item.name, item.name));
                    });
                    form.render('select');
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
            }
        });

        //获取当前用户的部门及人员结构树
        let treeList = getCurrentDepartmentUser();
        if (treeList && treeList.list) {
            let userTree = getAllJson(treeList.list);
            operationList=getxmselect("#operationCode", "operationCode", userTree);
        } else {
            operationList=getxmselect("#operationCode", "operationCode", [{name: user.name, value: user.code}])
        }
        //获取当前用户所属部门树()
        let currentDeptTreeList = getAllUserDepartmentTree();
        if (currentDeptTreeList && currentDeptTreeList.list) {
            let deptTree = getAllJson(currentDeptTreeList.list);
            deptOperationList = getxmselect("#deptOperationCode", "deptOperationCode", deptTree);
        } else {
            deptOperationList = getxmselect("#deptOperationCode", "deptOperationCode", [{name: user.name, value: user.code}]);
        }

    });



    form.render();//表单刷新
    let initData = [];
    table.render({
        elem: '#billing1Select'
        ,data:initData
        ,toolbar: '#billing1Toolbar'
        ,totalRow: true
        ,cols: [[
            {field: 'numbers',type:'numbers',title:'序号', totalRowText: '合计'}
            ,{field: 'manufacturerName', title: '厂商名称',width: 300}
            ,{field:'billingDateStr', title:'发票收到日期',sort:true,width: 150}
            ,{field:'invoiceCode', title:'报关发票号',width: 120}
            ,{field:'totalInvoicedAmount', title:'开票总额',sort:true,width: 120, totalRow: true}
            ,{field:'openedAmount', title:'已开金额',sort:true,width: 120, totalRow: true}
            ,{field:'uncommittedAmount', title:'未开金额',sort:true,width: 120, totalRow: true}
            ,{field:'createDateStr', title:'系统创建日期',sort:true,width: 150}
            ,{field:'billingTowerHeadName', title:'开票抬头',width: 120}
            ,{field:'operatorName', title:'操作员',width: 120}
            ,{field:'increaseCode', title:'增票号',width: 120}
            ,{field:'billDeclareInvoiceNo', title:'报关单号',width: 200}
            ,{field:'description', title:'备注',width: 200}

        ]]
        ,id: 'maininformationtableid'
        ,height: $(document).height() - $('#billing1Select').offset().top - 75,
        //开启分页
        page: true,
        limits: [300,600,900],
    });


//搜索提交
    form.on('submit(submitform)', function(data){
        table.reload('maininformationtableid', {
            page: {
                limit:300,
                curr: 1
            },
            url:'billing/billingPurchaseStatisticsListByPage',
            where:  data.field
        }, 'data');
        return false;
    });

    form.on('submit(billingExcel)', function (data) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: data.field,
            url:'billing/billingPurchaseStatisticsExport',
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 120,
                        'AJ': 120
                    }, 120);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '采购开票统计.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });
    document.getElementById('clear').onclick = function(){
        operationList.setValue([ ]);
        deptOperationList.setValue([ ]);
    };
});

